package com.woniuxy.demo.contorller;

import com.woniuxy.demo.entity.PageBean;
import com.woniuxy.demo.entity.ResponseResult;
import com.woniuxy.demo.entity.User;
import com.woniuxy.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 佘赛飞
 * @create 2021 -10 -12 8:22 PM
 */
@RestController
public class UserContorller {

    @Autowired
    UserService userService;
    @PostMapping("/login")
    public String login(@RequestBody User user){
        User userByName = userService.getUserByName(user.getUsername());
        if(userByName ==null){
            return "501";
        }else if(!user.getPassword().equals(userByName.getPassword())){
            return "502";
        }
        return "200";
    }
    //分页查询
    @GetMapping("/searchUsers")
    public ResponseResult<PageBean<User>>getAllUser(User user,int pageIndex, int pageSize){
        List<User>users=userService.getUsers(user,pageIndex,pageSize);
        int userCount = userService.getUserCount(user);
        System.out.println("查询："+user);
        PageBean<User> pageBean = new PageBean<>();
        pageBean.setBeanList(users);
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalRecord(userCount);
        return new ResponseResult<PageBean<User>>(pageBean,200,"ok");
    }
    //更新
    @PutMapping("/modifyUser")
    public ResponseResult modifyUser(@RequestBody User user){
        System.out.println("修改学生"+user);
        System.out.println(1111);
        return userService.updateUser(user);
    }

    //新增
    @PostMapping("/addUser")
    public ResponseResult addUser(@RequestBody User user){
        System.out.println(user);
        return userService.addUser(user);
    }

    @DeleteMapping("/removeUser/{uid}")
    public ResponseResult removeUser(@PathVariable("uid") int uid){
        return userService.deleteUser(uid);
    }
}
